package com.lsy.weizhi2.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.SelfMessageList;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.adapter.UnReadAdapter;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.utils.MyOkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 嗨哟 on 2017/5/6.
 */

public class ReadFragment extends Fragment {
    private Context context;
    private RecyclerView unread_rv;
    private UnReadAdapter unReadAdapter;
    private List<SelfMessageList.ListBean> list;
    private int pageNo;
    private Gson gson;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.unread_fragment,container,false);
        unread_rv = (RecyclerView) view.findViewById(R.id.unread_rv);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context=getActivity();
        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        unread_rv.setLayoutManager(manager);
        unReadAdapter = new UnReadAdapter(context,list);
        unread_rv.setAdapter(unReadAdapter);
        gson=new Gson();
        list=new ArrayList<>();
    }

    @Override
    public void onResume() {
        super.onResume();
        pageNo=1;
        getData();
    }

    private void getData(){
        Map<String,String> map=new HashMap<>();
        map.put("pageNo",pageNo+"");
        map.put("pageSize","10");
        MyOkHttpUtils.getInstance(context).doGet(Constants.SendPrivateMessage,map)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        Log.i("ReadFragment","message==="+message);
                        if(message!=null){
                            try {
                                JSONObject jsonObject=new JSONObject(message);
                                boolean result = jsonObject.getBoolean("result");
                                if(result){
                                    if(pageNo==1){
                                        list.clear();
                                    }
                                    SelfMessageList selfMessageList=gson.fromJson(message,SelfMessageList.class);
                                    list.addAll(selfMessageList.getList());
                                    unReadAdapter.UpData(list,0,0);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }
}
